package models

import (
	orm "go-papers/database"
	"go-papers/tools"
	_ "time"
)

type CommonCfgData struct {
	OrganRate    string `json:"organ_rate"`     //机构佣金比例
	MomRate      string `json:"mom_rate"`       //客户佣金
	MemberNums   string `json:"member_nums"`    //同小区注册用户数
	MomExtraRate string `json:"mom_extra_rate"` //佣金增长额为
	MomExtraMax  string `json:"mom_extra_max"`  // 客户佣上限为
}
type CommonCfg struct {
	CfgId     int    `json:"cfgId" gorm:"type:int(11) unsigned;primary_key"` //
	CfgCode   string `json:"cfgCode" gorm:"type:varchar(255);"`              //
	CfgName   string `json:"cfgName" gorm:"type:varchar(255);"`              // 配置名称
	CfgVal    string `json:"cfgVal" gorm:"type:varchar(255);"`               // 配置值
	CfgDesc   string `json:"cfgDesc" gorm:"type:varchar(255);"`              // 描述文案
	CreateBy  string `json:"createBy" gorm:"type:varchar(255);"`             //
	UpdateBy  string `json:"updateBy" gorm:"type:varchar(255);"`             //
	DataScope string `json:"dataScope" gorm:"-"`
	Params    string `json:"params"  gorm:"-"`
	BaseModel
}

type MoneyCfgData struct {
	OrganRate    float64 `json:"organ_rate"`     //机构佣金比例
	MomRate      float64 `jon:"mom_rate"`        //客户佣金
	MemberNums   int     `json:"member_nums"`    //同小区注册用户数
	MomExtraRate float64 `json:"mom_extra_rate"` //佣金增长额为
	MomExtraMax  float64 `json:"mom_extra_max"`  // 客户佣上限为
}

func (CommonCfg) TableName() string {
	return "common_cfg"
}

// 创建CommonCfg
func (e *CommonCfg) Create() (CommonCfg, error) {
	var doc CommonCfg
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

func (e *CommonCfg) GetCfg() MoneyCfgData {
	result, _ := e.GetListInfo()
	info := MoneyCfgData{}
	var err error
	if len(result) > 0 {
		for _, v := range result {
			if v.CfgCode == "organ_rate" {
				info.OrganRate = tools.StringToFloat64(err, v.CfgVal)
			}
			if v.CfgCode == "mom_rate" {
				info.MomRate = tools.StringToFloat64(err, v.CfgVal)
			}
			if v.CfgCode == "member_nums" {
				info.MemberNums, err = tools.StringToInt(v.CfgVal)
			}
			if v.CfgCode == "mom_extra_rate" {
				info.MomExtraRate = tools.StringToFloat64(err, v.CfgVal)
			}
			if v.CfgCode == "mom_extra_max" {
				info.MomExtraRate = tools.StringToFloat64(err, v.CfgVal)
			}
		}
	}
	return info
}

// 获取CommonCfg
func (e *CommonCfg) Get() (CommonCfg, error) {
	var doc CommonCfg
	table := orm.Eloquent.Table(e.TableName())
	if e.CfgId != 0 {
		table = table.Where("cfg_id = ?", e.CfgId)
	}
	if e.CfgCode != "" {
		table = table.Where("cfg_code = ?", e.CfgCode)
	}
	if e.CfgName != "" {
		table = table.Where("cfg_name = ?", e.CfgName)
	}
	if e.CfgVal != "" {
		table = table.Where("cfg_val = ?", e.CfgVal)
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

func (e *CommonCfg) GetListInfo() ([]CommonCfg, error) {
	var doc []CommonCfg
	table := orm.Eloquent.Select("*").Table(e.TableName())
	if e.CfgId != 0 {
		table = table.Where("cfg_id = ?", e.CfgId)
	}
	if e.CfgCode != "" {
		table = table.Where("cfg_code = ?", e.CfgCode)
	}
	if e.CfgName != "" {
		table = table.Where("cfg_name = ?", e.CfgName)
	}
	if e.CfgVal != "" {
		table = table.Where("cfg_val = ?", e.CfgVal)
	}

	if err := table.Find(&doc).Error; err != nil {
		return nil, err
	}

	return doc, nil

}

// 获取CommonCfg带分页
func (e *CommonCfg) GetPage(pageSize int, pageIndex int) ([]CommonCfg, int, error) {
	var doc []CommonCfg

	table := orm.Eloquent.Select("*").Table(e.TableName())
	if e.CfgId != 0 {
		table = table.Where("cfg_id = ?", e.CfgId)
	}
	if e.CfgCode != "" {
		table = table.Where("cfg_code = ?", e.CfgCode)
	}
	if e.CfgName != "" {
		table = table.Where("cfg_name = ?", e.CfgName)
	}
	if e.CfgVal != "" {
		table = table.Where("cfg_val = ?", e.CfgVal)
	}

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新CommonCfg
func (e *CommonCfg) UpdateByCode(code string) (update CommonCfg, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("cfg_code = ?", code).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 更新CommonCfg
func (e *CommonCfg) Update(id int) (update CommonCfg, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("cfg_id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除CommonCfg
func (e *CommonCfg) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("cfg_id = ?", id).Delete(&CommonCfg{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *CommonCfg) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("cfg_id in (?)", id).Delete(&CommonCfg{}).Error; err != nil {
		return
	}
	Result = true
	return
}
